<?php

/**
 * 大转盘
 */
class Api_Magic extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'getMagic'     => array(
				'live_type' => array('name' => 'live_type', 'type' => 'int', 'desc' => '直播类型 0视频直播 1语音聊天室'),
				'uid'       => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
			),
			'turn'         => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'id'      => array('name' => 'id', 'type' => 'int', 'desc' => '价格配置ID'),
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'desc' => '主播ID'),
				'stream'  => array('name' => 'stream', 'type' => 'string', 'desc' => '流名'),
				'times'   => array('name' => 'times', 'type' => 'int', 'desc' => '执行次数'),
				//'itype'   => array('name' => 'itype', 'type' => 'string', 'desc' => '魔法类型'),
			),
			'coinToCoupon' => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'  => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'coin'   => array('name' => 'coin', 'type' => 'int', 'default' => '1', 'desc' => '钻石'),
				'coupon' => array('name' => 'coupon', 'type' => 'int', 'desc' => '魔法券数量'),
			),
			'getWin'       => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页面'),
				'pnum'  => array('name' => 'pnum', 'type' => 'int', 'desc' => '每一页数量'),
				//'live_type' => array('name' => 'live_type', 'type' => 'int', 'desc' => '直播类型 0视频直播 1语音聊天室'),
			),
			'newWin'       => array(
				'p'    => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页面'),
				'pnum' => array('name' => 'pnum', 'type' => 'int', 'desc' => '每一页数量'),
				//'live_type' => array('name' => 'live_type', 'type' => 'int', 'desc' => '直播类型 0视频直播 1语音聊天室'),
			),
			'myMagic'      => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页面'),
				'pnum'  => array('name' => 'pnum', 'type' => 'int', 'desc' => '每一页数量'),
			),
		);
	}
	
	/**
	 * 召唤配置
	 * @desc 用于获取转盘配置
	 * @return int code 操作码，0表示成功
	 * @return array  info
	 * @return array  info[0].config  价格配置
	 * @return string info[0].config[].id
	 * @return string info[0].config[].times 次数
	 * @return string info[0].config[].coin 价格
	 * @return array  info[0].list  奖品列表
	 * @return string info[0].list[].id
	 * @return string info[0].list[].type 类型，0无奖1钻石2礼物
	 * @return string info[0].list[].type_val 类型值，type=0再接再厉 type=1钻石数量
	 * @return string info[0].list[].thumb 图片 type=1 钻石图标 type=2 礼物图标
	 * @return string msg 提示信息
	 */
	public function getMagic()
	{
		$live_type = checkNull($this->live_type);
		$domain    = new Domain_Magic();
		$con       = $domain->getConfig();
		$list      = $domain->getMagic($live_type);
		$user      = $domain->getUserCoupon($this->uid);
		$types     = ['common' => '普通', 'high' => '高级', 'luxury' => '豪华'];
		$info      = array('config' => $con, 'types' => $types, 'gift' => $list, 'user' => $user,);
		
		return $this->success([0 => $info]);
	}
	
	/**
	 * 召唤
	 * @desc 用于 用户抽奖
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].coin  余额
	 * @return array info[0].list  中奖列表
	 * @return string info[0].list[].id 奖品ID
	 * @return string info[0].list[].type 类型，0无奖1钻石2礼物
	 * @return string info[0].list[].type_val 类型值，type=0再接再厉 type=1钻石数量
	 * @return string info[0].list[].name 名称
	 * @return string info[0].list[].thumb 图片 type=1 钻石图标 type=2 礼物图标
	 * @return string info[0].list[].nums 数量
	 * @return string msg 提示信息
	 */
	public function turn()
	{
		//$itype   = checkNull($this->itype);
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$id      = checkNull($this->id);
		$liveuid = checkNull($this->liveuid);
		$stream  = checkNull($this->stream);
		$times   = checkNull($this->times);
		if($uid < 1 || $token == '' || $id < 0 || $times < 0 || $liveuid < 1 || $stream == ''){  // || $itype == ''
			return $this->fail('信息错误', 1000);
		}
		/*if(!in_array($itype, ['common', 'high', 'luxury'])){
			return $this->fail('模型类型错误', 1000);
		}*/
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		// 专票奖励
		$domain = new Domain_Magic();
		$res    = $domain->turn($uid, $id, $liveuid, $stream, $times);  // $itype,
		
		return $res;
	}
	
	
	// 魔法券兑换
	public function coinToCoupon()
	{
		$uid    = checkNull($this->uid);
		$token  = checkNull($this->token);
		$coin   = checkNull($this->coin);
		$coupon = checkNull($this->coupon);
		if($uid < 1 || $token == '' || $coin < 0 || $coupon < 0){
			return $this->fail('信息错误', 1000);
		}
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Magic();
		$res    = $domain->coinToCoupon($uid, $coin, $coupon);
		
		return $res;
	}
	
	/**
	 * 中奖记录
	 * @desc 用于获取中奖记录
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[].type 类型，0无奖1钻石2礼物
	 * @return string info[].type_val 类型值，type=0再接再厉 type=1钻石数量
	 * @return string info[].thumb 图片 type=1 钻石图标 type=2 礼物图标
	 * @return string info[].nums 数量
	 * @return string info[].addtime 时间
	 * @return string msg 提示信息
	 */
	public function getWin()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$p     = checkNull($this->p);
		$pnum  = checkNull($this->pnum);
		//$_type = checkNull($this->live_type);
		if($uid < 0 || $token == ''){
			return $this->fail('信息错误', 1000);
		}
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Magic();
		$list   = $domain->getWin($uid, $p, $pnum);
		
		return $this->success($list);
	}
	
	// 获取最新的获奖记录
	public function newWin()
	{
		$p      = checkNull($this->p);
		$pnum   = checkNull($this->pnum);
		$domain = new Domain_Magic();
		$list   = $domain->newWin(0, $p, $pnum);
		
		return $this->success($list);
	}
	
	// 我的召唤记录
	public function myMagic()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$p     = checkNull($this->p);
		$pnum  = checkNull($this->pnum);
		if($uid < 0 || $token == ''){
			return $this->fail('信息错误', 1000);
		}
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Magic();
		$list   = $domain->myMagic($uid, $p, $pnum);
		
		return $this->success([0 => $list]);
	}
	
}
